package com.xj.configuration;

import com.xj.interceptor.HostInfoInterceptor;
import com.xj.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Component
public class WebConfiguration implements WebMvcConfigurer {

  @Autowired
  private LoginInterceptor loginInterceptor;

  @Autowired
  private HostInfoInterceptor hostInfoInterceptor;

  /**
   * 先往浏览器的threadLocal中添加用户。如果没添加成功，则cookie必然没有登录。
   * 再判断Cookie中是否有用户。没有就加cookie，并添加浏览器的threadlocal中
   * @return
   */
  @Bean
  public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurer() {
      /**
       * 添加拦截器
       */
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        //这里判断是否需要免密登录
        registry.addInterceptor(hostInfoInterceptor).addPathPatterns("/**");
        //这里判断登录是否过期
        registry.addInterceptor(loginInterceptor).addPathPatterns("/book/**");
      }
    };
  }

}
